package com.wc.算法提高课.D第四章_高级数据结构.Floyd算法.牛站;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.PrintWriter;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;
import java.util.StringTokenizer;

/**
 * @Author congge
 * @Date 2024/10/15 23:39
 * @description https://www.acwing.com/problem/content/description/347/
 */
public class Main {
    /**
     * 思路：floyd的扩展<p>
     * 从新定义floyd的f[k, i, j],表示从i ~ j 恰好经过 k 条边的最短路<p>
     * 则恰好进过 n 条边的 = f[n, i, j], 假定n = a + b, f[n, i, j] = min{f[a, i, k] + f[b, k, j]}<p>
     * 计算经过几条边都是独立的, 可以通过 1 -> 2 -> 4 -> 8 ... 条边的最短路, 这样就可以利用倍增的思想计算经过 n 条边的最短路<p>
     * 因为floyd是 n^3 的时间复杂度, N < 1000,但是T < 100, 那么就需要离散化
     */
    static FastReader sc = new FastReader();
    static PrintWriter out = new PrintWriter(System.out);
    static int N = 210, INF = 0x3f3f3f3f;
    // g表示经过 1 一条边的
    static int[][] g = new int[N][N], res = new int[N][N];
    static Map<Integer, Integer> map = new HashMap<>();
    static int k, n = 0, m, S, E;

    public static void main(String[] args) {
        k = sc.nextInt();
        m = sc.nextInt();
        S = sc.nextInt();
        E = sc.nextInt();
        if (!map.containsKey(S)) map.put(S, ++n);
        if (!map.containsKey(E)) map.put(E, ++n);
        S = map.get(S);
        E = map.get(E);
        // 因为 g 之前表示第 0 条边, 正常应该需要是g[i][i] = 0, 但是我们需要存 1 条边的, 所以不需要初始化 g[i][i] = 0
        for (int i = 1; i < N; i++) Arrays.fill(g[i], INF);
        while (m-- > 0) {
            int c = sc.nextInt(), a = sc.nextInt(), b = sc.nextInt();
            if (!map.containsKey(a)) map.put(a, ++n);
            if (!map.containsKey(b)) map.put(b, ++n);
            a = map.get(a);
            b = map.get(b);
            g[a][b] = g[b][a] = Math.min(g[a][b], c);
        }
        qmi();
        out.println(res[S][E]);
        out.flush();
    }

    static void qmi() {
        // res 开始存的是 0 条边, 所以res[i][i] = 0;
        for (int i = 1; i <= n; i++) {
            Arrays.fill(res[i], 1, n + 1, INF);
            res[i][i] = 0;
        }
        while (k > 0) {
            if ((k & 1) == 1) mul(res, res, g);
            // 这个就表示g = g ^ 2
            mul(g, g, g);
            k >>= 1;
        }
    }

    static void mul(int[][] c, int[][] a, int[][] b) {
        int[][] tmp = new int[N][N];
        for (int i = 1; i <= n; i++) Arrays.fill(tmp[i], 1, n + 1, INF);
        for (int k = 1; k <= n; k++) {
            for (int i = 1; i <= n; i++) {
                for (int j = 1; j <= n; j++) {
                    tmp[i][j] = Math.min(tmp[i][j], a[i][k] + b[k][j]);
                }
            }
        }
        for (int i = 1; i <= n; i++) {
            System.arraycopy(tmp[i], 1, c[i], 1, n);
        }
    }
}

class FastReader {
    StringTokenizer st;
    BufferedReader br;

    FastReader() {
        br = new BufferedReader(new InputStreamReader(System.in));
    }

    String next() {
        while (st == null || !st.hasMoreElements()) {
            try {
                st = new StringTokenizer(br.readLine());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return st.nextToken();
    }

    int nextInt() {
        return Integer.parseInt(next());
    }

    String nextLine() {
        String s = "";
        try {
            s = br.readLine();
        } catch (IOException e) {
            e.printStackTrace();
        }
        return s;
    }

    long nextLong() {
        return Long.parseLong(next());
    }

    double nextDouble() {
        return Double.parseDouble(next());
    }

    // 是否由下一个
    boolean hasNext() {
        while (st == null || !st.hasMoreTokens()) {
            try {
                String line = br.readLine();
                if (line == null)
                    return false;
                st = new StringTokenizer(line);
            } catch (IOException e) {
                throw new RuntimeException(e);
            }
        }
        return true;
    }
}
